package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/list")
    public Result list(@RequestParam("categoryId") long categoryId) {
        //设置redis的key值
        String key = "dish_" + categoryId;
        //判断redis中是否有缓存
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);

        if(list != null && list.size() > 0) {

            return Result.success(list);
        }
        //根据分类id查询菜品
        Dish dish = new Dish();
        //查询起售状态的菜品
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCategoryId(categoryId);
        list = dishService.listWithFlavor(dish);
        //将查询结果存入redis
        redisTemplate.opsForValue().set(key, list);
        //返回list
       return Result.success(list);
    }
}
